import { constantRouterMap, asyncRouterMap } from '@/router/router.config'
import { getMaxFloor } from '@/utils/TreeUtil'
// 防刷新插件
// import PersistedState from 'vuex-persistedstate'
/**
 * 过滤账户是否拥有某一个权限，并将菜单从加载列表移除
 *
 * @param permissions 角色编码 ['user']
 * @param route 单个路由
 * @returns {boolean}
 */
function hasPermission(permissions, route) {

    // 过滤菜单
    if (route.meta && route.meta.permission) {
        for (let i = 0, len = permissions.length; i < len; i++) {
            if (permissions[i] === route.meta.permission) {
                return true
            }
        }
        return false
    }

    // 过滤空目录
    if (route.children != undefined && route.children.length === 0) {
        return false
    }

    return true
}

/**
 * 过滤路由菜单
 * @param {*} routerMap 待过滤路由
 * @param {*} permissions 角色编码 ['user']
 * @returns 
 */
function filterAsyncRouter(routerMap, permissions) {
    const accessedRouters = routerMap.filter(route => {
        if (hasPermission(permissions, route)) {
            if (route.children && route.children.length) {
                route.children = filterAsyncRouter(route.children, permissions)
            }
            return true
        }
        return false
    })
    return accessedRouters
}

/**
 * 转换接口返回的资源数据，转换为 ['sysUser',....]
 * @returns 
 */
function convertPermission(permissionDtoList) {
    let permissions = []
    permissionDtoList.forEach(x => {
        permissions.push(x.permissionCode)
    })
    console.log('当前用户角色资源：' + permissions);
    return permissions
}

const permission = {
    state: {
        routers: constantRouterMap, // 所有路由
        addRouters: [], // 待添加路由
        permission: [], // 菜单资源编码 ['SysUser']
        operationPermission: [] // 操作资源编码 { permissionCode: 'reset', url: '/sys/user' ....}
    },

    mutations: {
        /**
          * 设置路由
          * @param {*}} param0 
          * @param {*} data 
          * @returns 
          */
        SET_ROUTERS: (state, data) => {
            state.addRouters = data
            state.routers = constantRouterMap.concat(data)
        },
        /**
          * 设置角色资源
          * @param {*}} param0 
          * @param {*} data 
          * @returns 
          */
        SET_PERMISSION: (state, data) => {
            state.permission = data
        },
        /**
          * 设置操作权限
          * @param {*}} param0 
          * @param {*} data 
          * @returns 
          */
        SET_OPERATION_PERMISSION: (state, data) => {
            state.operationPermission = data
        }
    },

    actions: {
        /**
         * 根据角色资源编码设置路由
         * @param {*}} param0 
         * @param {*} data 
         * @returns 
         */
        GenerateRoutes({ commit }, data) {
            return new Promise(resolve => {
                const permission = convertPermission(data.permissionDtoList)
                let accessedRouters
                for (let i = 0; i <= getMaxFloor(asyncRouterMap); i++) {
                    // 根据资源编码过滤菜单
                    accessedRouters = filterAsyncRouter(asyncRouterMap, permission)
                }
                commit('SET_ROUTERS', accessedRouters)
                commit('SET_PERMISSION', permission)
                commit('SET_OPERATION_PERMISSION', data.permissions)
                resolve(accessedRouters)
            })
        },
    },
    // 注入插件
    plugins: [
        // 注入防刷新插件
        // PersistedState({ storage: window.sessionStorage })
    ]

}


export default permission